import Vue from 'vue'
import axios from 'axios'
import { getToken, removeToken } from '../utile/token'
import router from '../router/index'
axios.defaults.baseURL = process.env.VUE_APP_BASEURL
axios.defaults.withCredentials = true
axios.interceptors.request.use(
  function (config) {
    const token = getToken()
    if (token) {
      config.headers.token = token
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)
axios.interceptors.response.use(
  function (response) {
    if (response.data.code === 206) {
      removeToken()
      router.push('/login')
    }
    return response.data
  },
  function (error) {
    return Promise.reject(error)
  }
)
Vue.prototype.$axios = axios
